﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Fabrication.Extensions;
using Fabrication.ValueProviders;

namespace Fabrication
{
    public static class BooleanRuleExtensions
    {
        private static readonly List<bool> boolOptions = new List<bool> { true, false };
        private static readonly Dictionary<bool, bool> boolNegativeMaping = new Dictionary<bool, bool> { {true, false}, {false, true} };

        public static IBuilderOptions<T> Negate<T>(this PropertySelector<T, bool> targetPropertySelector, Expression<Func<T, bool>> sourceMember)
        {
            var builderOptions = targetPropertySelector.BuilderOptions;
            var sourcePropertyName = sourceMember.GetPropertyName();
            var targetPropertyName = targetPropertySelector.PropertyName;

            builderOptions.AddMap(typeof(T), sourcePropertyName, targetPropertyName, boolNegativeMaping);

            return builderOptions;
        }

        public static IBuilderOptions<T> Randomize<T>(this PropertySelector<T, bool> targetPropertySelector)
        {
            var builderOptions = targetPropertySelector.BuilderOptions;
            var key = new TypedPropertyKey(typeof(T), targetPropertySelector.PropertyName);
            builderOptions.AddPropertyValueProvider(key, new AllValueProvider<bool>(boolOptions));
            return builderOptions;
        }
    }
}